home *** CD-ROM | disk | FTP | other *** search
/ PCMania 75 / PCMania CD75_1.iso / lycos / netscape / netcast.z / ncjs10.jar / btnbar.js < prev    next >
Text File  |  1997-11-26  |  6KB  |  261 lines

  1. var buttonSlopX = 5;
  2. var buttonSlopY = 2;
  3. var firstButtonX = 5;
  4.  
  5. var prev = "";
  6. var current;
  7.  
  8. function Point (x, y) {
  9.     this.x = x;
  10.     this.y = y;
  11. }
  12.  
  13.  
  14. function ButtonSetup(name, value, tip, clickAction)
  15. {
  16.     this.name = name;
  17.     this.value = value;
  18.     this.tip = tip;
  19.     this.clickAction = clickAction;
  20. }
  21.  
  22.  
  23. function buttonHilite()
  24. {
  25.     var hilite = window.document.layers['topHilite'];
  26.     var bottomHilite = window.document.layers['bottomHilite'];
  27.  
  28.     hilite.left = this.left-buttonSlopX;
  29.     hilite.top = this.top-buttonSlopY;
  30.     hilite.clip.width=this.clip.width + (2 * buttonSlopX) -1;
  31.     hilite.clip.height=this.clip.height+buttonSlopY;
  32.     hilite.visibility="show";
  33.     
  34.     bottomHilite.left = this.left + hilite.clip.width + 1 - buttonSlopX - bottomHilite.document.width;
  35.     bottomHilite.top = this.top + hilite.clip.height - bottomHilite.document.height;
  36.     bottomHilite.clip.left = bottomHilite.document.width - hilite.clip.width;
  37.     bottomHilite.clip.top = bottomHilite.document.height - hilite.clip.height;
  38.     bottomHilite.visibility="show";
  39.  
  40.     return;
  41. }
  42.  
  43. function buttonDim()
  44. {
  45.     window.document.layers['topHilite'].visibility="hidden";
  46.     window.document.layers['bottomHilite'].visibility="hidden";
  47.     return;
  48. }
  49.  
  50. function buttonCreate()
  51. {
  52.     var buttonLayer;
  53.  
  54.     buttonLayer = new Layer(this.width + 2);
  55.     buttonLayer.visibility = "hide";
  56.     buttonLayer.document.open();
  57.     var outputString = "";
  58.     outputString += "<layer top=0 left=0 clip=50,30><a href='gencon.htm'";
  59.     outputString += " onclick=\"self.buttonClick('";
  60.     outputString += this.value;
  61.     outputString += "'); return false;\">";
  62.     outputString += "<img src=\"images/blank.gif\" border=0 width=50 height=30 alt=\"";
  63.     outputString += this.tooltip;
  64.     outputString += "\"></a></layer>";
  65.     outputString += "<font face = \"" + this.fontFace + "\" point-size = " + this.fontSize + ">";
  66.     outputString += this.text;
  67.     outputString += "</font>";
  68.     buttonLayer.document.write(outputString);
  69.  
  70.     buttonLayer.document.close();
  71.  
  72.     buttonLayer.left = this.xPos;
  73.     buttonLayer.top = this.yPos;
  74.     buttonLayer.clip.width = this.width;
  75.     buttonLayer.clip.height = this.height;
  76.     buttonLayer.onMouseOver = this.hilite;
  77.     buttonLayer.onMouseOut = this.dim;
  78.  
  79.     return buttonLayer;
  80. }
  81.  
  82. function TextButton(parent, value, text, tooltip, width, height, xPos, yPos, font, size)
  83. {
  84.     this.value = value;
  85.     this.text = text;
  86.     this.tooltip = tooltip;
  87.     this.height= height;
  88.     this.width = width;
  89.     this.xPos = xPos;
  90.     this.yPos = yPos;
  91.     this.fontFace = font;
  92.     this.fontSize = size.toString();
  93.     this.parent = parent;
  94.  
  95.     this.createButton = buttonCreate;
  96.     this.hilite = buttonHilite;
  97.     this.dim = buttonDim;
  98.     this.click = '"' + buttonClick + '"';
  99. }
  100.  
  101. function measureText(text, font, fontSize) 
  102. {
  103.     outputString = "";
  104.  
  105.     document.layers['measure'].document.clear();
  106.     document.layers['measure'].document.open();
  107.     outputString += "<font face = \"" + font + "\" point-size = \"" + fontSize + "\">";
  108.     outputString += text;
  109.     outputString += "</font>";
  110.     document.layers['measure'].document.write(outputString);     
  111.     document.layers['measure'].document.close();
  112.  
  113.     var size = new Point(document.layers['measure'].clip.width, document.layers['measure'].clip.height);
  114.  
  115.     return size;
  116. }
  117.  
  118.  
  119. function layoutButtons (specArray)
  120. {
  121.     var size = 0 ;
  122.     var rowWidth = 0;
  123.     var rowNum = 0;
  124.     var maxHeight =0 ;
  125.     var font = specArray.fontFace;
  126.     var fontSize = specArray.fontSize;
  127.     var startHeight = specArray.startHeight;
  128.  
  129.     var rows = new Array();
  130.     var buttons = new Array();
  131.  
  132.     var name;
  133.     var button, row;
  134.     var spaceWidth;
  135.     var xPos = firstButtonX + buttonSlopX;
  136.     var i;
  137.     var retryCount = 0;
  138.  
  139.     for (i=0; i< specArray.length; i++) {
  140.         name = specArray[i].name;
  141.         size = measureText(name, font, fontSize);
  142.  
  143.         if (size.y > maxHeight)
  144.             maxHeight = size.y;
  145.  
  146.         if (rowWidth + size.x > 190) {
  147.             if (retryCount >= 1) {
  148.                 // exceeded the width of the row, and we've already tried to re-layout
  149.                 // the buttons.  We'll just have to do drop to another line.
  150.  
  151.                 buttons.rowWidth = rowWidth;
  152.                 rows[rowNum++] = buttons;
  153.                 buttons = new Array();
  154.                 startHeight = 3;
  155.                 rowWidth = 0;
  156.             } else {
  157.                 // exceeded the width of our space.  Let's try it by dropping the
  158.                 // font size by a bit and try to relayout.
  159.  
  160.                 fontSize -=2;
  161.                 retryCount ++;
  162.  
  163.                 i=-1;
  164.  
  165.                 size = 0 ;
  166.                 rowWidth = 0;
  167.                 rowNum = 0;
  168.                 maxHeight =0 ;
  169.  
  170.                 buttons.length = 0;
  171.                 continue;
  172.             }
  173.         }
  174.  
  175.         rowWidth += size.x;
  176.         buttons[buttons.length] = new TextButton(null, specArray[i].value, name, specArray[i].tip, size.x, size.y, -1, -1, font, fontSize);
  177.     }
  178.  
  179.     buttons.rowWidth = rowWidth;
  180.     rows[rowNum] = buttons;
  181.  
  182.     rowNum=0;
  183.     for (row in rows) {
  184.         rowWidth = rows[row].rowWidth;
  185.         spacerWidth = (180 - rowWidth) / (rows[row].length - 1);
  186.         buttons = rows[row];
  187.         for (i=0; i < buttons.length; i++) {
  188.             button = buttons[i];
  189.             button.xPos = xPos;
  190.             button.yPos = startHeight + (rowNum * maxHeight);
  191.             xPos += button.width + spacerWidth;
  192.         }
  193.  
  194.         xPos = firstButtonX + buttonSlopX;
  195.         rowNum++;
  196.     }
  197.  
  198.     rows.totalHeight = rowNum * maxHeight;
  199.     return rows;
  200. }
  201.  
  202. function MakeButtons(buttons)
  203. {
  204.     var rows;
  205.     var rowButtons;
  206.     var button;
  207.     var visButton;
  208.     rows = layoutButtons(buttons);
  209.  
  210.     for (var row in rows) {
  211.         rowButtons = rows[row];
  212.         for (i=0; i < rowButtons.length; i++) {
  213.             button = rowButtons[i];
  214.  
  215.             visButton = button.createButton();
  216.             visButton.visibility="show";
  217.         }
  218.     }
  219. }
  220.  
  221. function buttonClick(button)
  222. {
  223.     switch (button) {
  224.         case "New":
  225.             HandleAdd();
  226.             break;
  227.         case "Options":
  228.             HandleEdit();
  229.             break;
  230.         case "Help":
  231.             HandleHelp("");
  232.             break;
  233.         case "Exit":
  234.             HandleExit();
  235.             break;
  236.     }    
  237.  
  238. }
  239.  
  240. var buttons = new Array(4);
  241.  
  242. buttons[0] = new ButtonSetup(top.getLocalString("New"), "New", top.getLocalString("Add a new item to Netcaster"), "");
  243. buttons[1] = new ButtonSetup(top.getLocalString("Options"), "Options", top.getLocalString("Configure Netcaster"), "");
  244. buttons[2] = new ButtonSetup(top.getLocalString("Help"), "Help", top.getLocalString("Get Help"), "");
  245. buttons[3] = new ButtonSetup(top.getLocalString("Exit"), "Exit", top.getLocalString("Close Netcaster"), "");
  246.  
  247. buttons.startHeight = 7;
  248. if (depth.isWindows()) {
  249.     buttons.fontFace = "Arial";
  250. } else {
  251.     buttons.fontFace = "Helvetica";
  252. }
  253. buttons.fontSize = "10";
  254.  
  255. function onLoad() {
  256.     MakeButtons(buttons);
  257. }
  258.  
  259. compromisePrincipals();
  260.  
  261. void(0);